//
//  CourseViewLogin.m
//  xpkc
//
//  Created by TopSage on 15/10/12.
//  Copyright (c) 2015年 TopSage. All rights reserved.
//

#import "CourseViewLogin.h"
#import "UIView+Shake.h"
@interface CourseViewLogin (){
    UITextField *username;
    UITextField *password;
    UIButton *loginButton;

}

@end

@implementation CourseViewLogin

- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self creatCourseLoginView];
    }
    return self;
}

- (void)creatCourseLoginView{
    self.backgroundColor = [UIColor whiteColor];
    //frame
    CGRect frame_studypay;
    CGRect frame_content;
    CGRect frame_usericon;
    CGRect frame_pswicon;
    CGRect frame_username;
    CGRect frame_password;
    CGRect frame_line;
    CGRect frame_showbtn;
    CGRect frame_loginBtn;
    CGRect frame_registBtn;
    CGRect frame_forgotBtn;
    
    CGFloat offset;
  
        offset = 20;
        
        frame_studypay  = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH/3);
        frame_content   = CGRectMake(offset, SCREEN_WIDTH/3+offset/2, SCREEN_WIDTH-offset*2, 100);
        frame_line      = CGRectMake(offset/2, 50, frame_content.size.width-offset, 1);
        frame_usericon  = CGRectMake(offset/2, 15, 20, 20);
        frame_pswicon = CGRectMake(offset/2, 65, 20, 20);
        frame_username  = CGRectMake(offset+20, 8, frame_content.size.width-offset*2 - 15, 40);
        frame_password  = CGRectMake(offset+20, 55, frame_content.size.width-offset*2 - 20 - 30, 40);
        frame_showbtn   = CGRectMake(frame_content.size.width - 24 - offset, 65, 32, 24);
        frame_loginBtn  = CGRectMake(offset, frame_content.origin.y+frame_content.size.height+offset*1.5, SCREEN_WIDTH-offset*2, 50);
        frame_registBtn = CGRectMake(offset, frame_loginBtn.origin.y+frame_loginBtn.size.height+offset, 70, 20);
        frame_forgotBtn = CGRectMake(SCREEN_WIDTH-70-offset, frame_loginBtn.origin.y+frame_loginBtn.size.height+offset, 70, 20);
    
    
    
    
    UIImageView *studypay = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    studypay.frame = frame_studypay;
    studypay.image = [UIImage imageNamed:@"studypay_green"];
    [self addSubview:studypay];
    
    UIView *content = [[UIView alloc]initWithFrame:frame_content];
    content.backgroundColor = CCOLOR;
    content.layer.borderWidth = 1;
    content.layer.cornerRadius = 10;
    content.layer.borderColor = [UIColor lightGrayColor].CGColor;
    [self addSubview:content];
    
    UIImageView *usericon = [[UIImageView alloc]initWithFrame:frame_usericon];
    usericon.image = [UIImage imageNamed:@"user_gray"];
    [content addSubview:usericon];
    
    UIImageView *pswicon = [[UIImageView alloc]initWithFrame:frame_pswicon];
    pswicon.image = [UIImage imageNamed:@"password_gray"];
    [content addSubview:pswicon];
    
    username = [[UITextField alloc]initWithFrame:frame_username];
    username.clearButtonMode = UITextFieldViewModeWhileEditing;
    username.placeholder = @"请输入用户名";
    username.text = [[NSUserDefaults standardUserDefaults]valueForKey:USERNAME_REMBER];
    [content addSubview:username];
    
    password = [[UITextField alloc]initWithFrame:frame_password];
    password.secureTextEntry = YES;
    password.clearsOnBeginEditing = YES;
    password.placeholder = @"请输入密码";
    password.text = [[NSUserDefaults standardUserDefaults]valueForKey:PASSWORD_REMBER];
    [content addSubview:password];
    
    UIView *line = [[UIView alloc]initWithFrame:frame_line];
    line.backgroundColor = [UIColor lightGrayColor];
    [content addSubview:line];
    
    UIButton *showBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    showBtn.frame = frame_showbtn;
    [showBtn setImage:[UIImage imageNamed:@"password_show_blue"] forState:UIControlStateNormal];
    [showBtn setImage:[UIImage imageNamed:@"password_hide_blue"] forState:UIControlStateSelected];
    [showBtn addTarget:self action:@selector(showOrHidePassword:) forControlEvents:UIControlEventTouchUpInside];
    [content addSubview:showBtn];
    

    loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
    loginButton.frame = frame_loginBtn;
    loginButton.backgroundColor = OrangeColor;
    loginButton.layer.cornerRadius = 10;
    [loginButton setTitle:@"登  录" forState:UIControlStateNormal];
    [loginButton setTitle:@"登录中" forState:UIControlStateSelected];
    [loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [loginButton.titleLabel setShadowOffset:CGSizeMake(0.5, 0.5)];
    [loginButton setTitleShadowColor:[UIColor whiteColor] forState:UIControlStateNormal];
    loginButton.titleLabel.font = [UIFont boldSystemFontOfSize:18];
    [loginButton addTarget:self action:@selector(loginBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:loginButton];
    
    UIButton *registButton = [UIButton buttonWithType:UIButtonTypeCustom];
    registButton.frame = frame_registBtn;
    [registButton setTitle:@"快速注册" forState:UIControlStateNormal];
    [registButton setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    registButton.titleLabel.font = [UIFont boldSystemFontOfSize:15];
    [registButton addTarget:self action:@selector(registBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:registButton];
    
    UIButton *forgotButton = [UIButton buttonWithType:UIButtonTypeCustom];
    forgotButton.frame = frame_forgotBtn;
    [forgotButton setTitle:@"找回密码" forState:UIControlStateNormal];
    [forgotButton setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    forgotButton.titleLabel.font = [UIFont boldSystemFontOfSize:15];
    [forgotButton addTarget:self action:@selector(forgotBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:forgotButton];
    
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(closeKeyBoard)];
    [self addGestureRecognizer:tap];
}

#pragma mark - func
- (void)showOrHidePassword:(UIButton *)btn{
    if (btn.selected) {
        password.secureTextEntry = YES;
        btn.selected = NO;
    }else{
        password.secureTextEntry = NO;
        btn.selected = YES;
    }
}

- (void)closeKeyBoard{
    [username resignFirstResponder];
    [password resignFirstResponder];
}

- (void)basicCheck{
    //基本检查
    //用户名字数
    //密码字数
    //特殊文字
    NSString *userName = username.text;
    NSString *passWord = password.text;
    for (int i = 0; i<userName.length; i++) {
        unichar ch = [userName characterAtIndex:i];
        if (0x4e00 < ch  && ch < 0x9fff)
        {
            logdebug(@"汉字");
        }
        if ('0' <= ch && ch <= '9') {
            logdebug(@"数字");
        }
        if ('A' <= ch && ch <= 'z') {
            logdebug(@"字母");
        }
    }
    if ([self.delegate respondsToSelector:@selector(loginWithUsername:andPassword:)]) {
        [self.delegate loginWithUsername:userName andPassword:passWord];
        loginButton.selected = YES;
    }
}
- (void)loginBtnClick{
    [self closeKeyBoard];
    if (loginButton.selected) {
        return;
    }
    [self basicCheck];
}
- (void)registBtnClick{
    [self closeKeyBoard];
    if ([self.delegate respondsToSelector:@selector(regist)]) {
        [self.delegate regist];
    }
}
- (void)forgotBtnClick{
    [self closeKeyBoard];
    if ([self.delegate respondsToSelector:@selector(forgot)]) {
        [self.delegate forgot];
    }
}
- (void)loginStatue:(LoginStatue)statue{
    loginButton.selected = NO;
    
    switch (statue) {
        case LoginStatueSucc:
        {
            logdebug(@"登录成功");
        }
            break;
        case LoginStatueUsernameError:
        {
            [username shake:5 withDelta:6 andSpeed:0.04 shakeDirection:ShakeDirectionHorizontal];
        }
            break;
        case LoginStatuePasswordError:
        {
            [password shake:5 withDelta:6 andSpeed:0.04 shakeDirection:ShakeDirectionHorizontal];
        }
            break;
            
        default:
            break;
    }
}

- (void)fillUsername:(NSString *)userName andPassword:(NSString *)passWord{
    username.text = userName;
    password.text = passWord;
}
@end
